import http from './http';

// 获取用户订单
export const getOrders = async (page = 0, size = 10) => {
  try {
    const response = await http.get(`/orders?page=${page}&size=${size}`);
    return response;
  } catch (error) {
    return error;
  }
};

// 搜索用户订单
export const searchOrders = async (queryParams) => {
  try {
    const { page = 0, size = 10, startTime, endTime, bookName } = queryParams;
    let url = `/orders/search?page=${page}&size=${size}`;
    
    if (startTime) url += `&startTime=${encodeURIComponent(startTime)}`;
    if (endTime) url += `&endTime=${encodeURIComponent(endTime)}`;
    if (bookName) url += `&bookName=${encodeURIComponent(bookName)}`;
    
    const response = await http.get(url);
    return response;
  } catch (error) {
    return error;
  }
};

// 获取所有订单（管理员功能）
export const getAllOrders = async (queryParams) => {
  try {
    const { page = 0, size = 10, startTime, endTime, bookName } = queryParams;
    let url = `/orders/all?page=${page}&size=${size}`;
    
    if (startTime) url += `&startTime=${encodeURIComponent(startTime)}`;
    if (endTime) url += `&endTime=${encodeURIComponent(endTime)}`;
    if (bookName) url += `&bookName=${encodeURIComponent(bookName)}`;
    
    const response = await http.get(url);
    return response;
  } catch (error) {
    return error;
  }
};

// 获取订单详情
export const getOrderById = async (id) => {
  try {
    const response = await http.get(`/orders/${id}`);
    return response;
  } catch (error) {
    return error;
  }
};

// 创建订单（从购物车）
export const createOrder = async () => {
  try {
    const response = await http.post('/orders');
    return response;
  } catch (error) {
    return error;
  }
};

// 获取书籍销量统计（管理员功能）
export const getBookSalesStatistics = async (startTime, endTime) => {
  try {
    let url = '/orders/statistics/book-sales';
    if (startTime || endTime) {
      url += '?';
      if (startTime) url += `startTime=${encodeURIComponent(startTime)}`;
      if (startTime && endTime) url += '&';
      if (endTime) url += `endTime=${encodeURIComponent(endTime)}`;
    }
    
    const response = await http.get(url);
    return response;
  } catch (error) {
    return error;
  }
};

// 获取用户消费统计（管理员功能）
export const getUserConsumptionStatistics = async (startTime, endTime) => {
  try {
    let url = '/orders/statistics/user-consumption';
    if (startTime || endTime) {
      url += '?';
      if (startTime) url += `startTime=${encodeURIComponent(startTime)}`;
      if (startTime && endTime) url += '&';
      if (endTime) url += `endTime=${encodeURIComponent(endTime)}`;
    }
    
    const response = await http.get(url);
    return response;
  } catch (error) {
    return error;
  }
};

// 获取用户自己的购书统计
export const getUserBookStatistics = async (startTime, endTime) => {
  try {
    let url = '/orders/statistics/my-books';
    if (startTime || endTime) {
      url += '?';
      if (startTime) url += `startTime=${encodeURIComponent(startTime)}`;
      if (startTime && endTime) url += '&';
      if (endTime) url += `endTime=${encodeURIComponent(endTime)}`;
    }
    
    const response = await http.get(url);
    return response;
  } catch (error) {
    return error;
  }
}; 